package test2;

//产品抽象类
abstract class Product {
 public abstract void use();
}

//具体产品类A
class ConcreteProductA extends Product {
 @Override
 public void use() {
     System.out.println("使用ConcreteProductA");
 }
}

//具体产品类B
class ConcreteProductB extends Product {
 @Override
 public void use() {
     System.out.println("使用ConcreteProductB");
 }
}

//简单工厂类
class SimpleFactory {
 public static Product createProduct(String type) {
     if ("A".equals(type)) {
         return new ConcreteProductA();
     } else if ("B".equals(type)) {
         return new ConcreteProductB();
     }
     throw new IllegalArgumentException("商品类型无效");
 }
}

//测试简单工厂模式
public class SimpleFactoryPatternTest {
 public static void main(String[] args) {
     Product productA = SimpleFactory.createProduct("A");
     productA.use();

     Product productB = SimpleFactory.createProduct("B");
     productB.use();
 }
}